﻿addLoadFuc(function () {
    initPageObjects();
});

//更改活动名称输入框的颜色
function changeColor(ipt) {
    ipt.style.backgroundColor = "#fff";
}

//更改活动名称输入框的颜色
function changeDefaultColor(ipt) {
    ipt.style.backgroundColor = "#f5f5f5";
}

//修改活动的名称
function modifyName() {
    var value = $id("id_ipt_attr_name").value;
    var actid = $id("id_ipt_attr_id").value;
    if (actid == "") return;
    var act = $id(actid);
    act.setAttribute("name", value);
    
    var curSelectedAct = $id(actid.replace(prefixWfActId, prefixDivActId));
    if (act.getAttribute(attrType) != typeCommon) return;
    
    if (value.length > 6) {
        curSelectedAct.innerHTML = value;
        curSelectedAct.setAttribute("class", curSelectedAct.className.replace("actname", "actnameSmall"));
    } else {
        curSelectedAct.innerHTML = value;
        curSelectedAct.setAttribute("class", curSelectedAct.className.replace("actnameSmall", "actname"));
    }
    
}

//@思恒 点击活动时,间相关属性填充到右侧的属性框中
function mageMttrs(targetElement) {
   // if (targetElement == null || targetElement.tagName.toUpperCase() != "DIVWF") return;
    
    targetElement = event.srcElement;
    if (targetElement.tagName.toUpperCase() != "DIVWF") return;

    var act = $id(targetElement.getAttribute("id").replace(prefixDivActId,prefixWfActId));

    //当修改属性框属性时,修改活动的属性
    $id("id_ipt_attr_id").value = act.getAttribute(attrId);
    $id("id_ipt_attr_name").value = act.getAttribute(attrName);
    
   
    //设置输入输出select的选项
    if (act.getAttribute(attrIn) == varlueFalse) {
        $id("id_sel_attr_in").selectedIndex = 1;
    } else {
        $id("id_sel_attr_in").selectedIndex = 0;
    }

    if (act.getAttribute(attrOut) == varlueFalse) {
        $id("id_sel_attr_out").selectedIndex = 1;
    } else {
        $id("id_sel_attr_out").selectedIndex = 0;
    }

    //----------列出路径----------
    var tb = $id("id_tb_attr_acts").children[0]; //获取tbody
    var count = tb.childNodes.length;
    for (var i = 1; i < count; i++) {
        tb.removeChild(tb.childNodes[1]);
    }
    var wfpath = act.querySelectorAll(tagwfnextact);
    count = wfpath.length;
    if (count > 0) {
        for (var i = 0; i < count; i++) {
          //  if (wfpath[i].getAttribute(attrNextActId) == act.id) {
                //------------找到路径后添加到右侧属性列表
                var newRow = tb.insertRow(1); // 插入一行
                var cel = newRow.insertCell(0);
                cel.innerHTML = wfpath[i].getAttribute(attrNextActId);//下一活动的id(隐藏)
                cel.style.display = "none";
                
                cel = newRow.insertCell(1);
                cel.innerHTML = $id(wfpath[i].getAttribute(attrNextActId)).getAttribute(attrName);//获取下一活动名称
                //如果是条件分支活动 则插入条件(如果条件存在的话
                if (act.getAttribute(attrType) ==typeRouteOrSplit) {
                    newRow.insertCell(2).innerHTML = " <label onclick=\"showConditionEditor(this)\" style='width:100px;'>" + (wfpath[i].innerHTML == "" ? "请填写条件" : wfpath[i].innerHTML) + "</label>";//"<input type='text' value='"+wfpath[i].innerHTML+"' onchange=\"setCondition(this)\" style='width:100px;'  />";
                } else {
                    newRow.insertCell(2).innerHTML = "";
                }
                newRow.insertCell(3).innerHTML = "<label onclick=\"deletePath(this)\">X</label>";
           // }///
        }
    }

    $id("id_ipt_attr_module").value = act.querySelector(tagwfdesc).innerHTML;
    
    //--------列出授权用户名
    var wfus = act.querySelectorAll(tagwfuser);
    if (wfus && wfus.length > 0) {
        var innerhtmls = "";
        for (var j = 0; j < wfus.length; j++) {
            innerhtmls += wfus[j].querySelector(tagwfdesc).innerHTML + "<br />";
        }
        $id("lb_user_names").innerHTML = innerhtmls;
    } else {
        $id("lb_user_names").innerHTML = "";
    }
}

function btnOK(parameters) {
    if ($id("lb_ident").innerHTML == "PATH") {
        ModifyPath();
    } else if ($id("lb_ident").innerHTML == "MODULE") {
        btnModuleConfirm();
    }
    $id("lb_ident").innerHTML = "";
}

function btnCancel() {
    clearTemp();
    $id("div_float_container").style.display = "none";
}

//输入逻辑表达式 
function showConditionEditor(lb) {
    var v = prompt("输入表达式:");
    if (!v) return;
    var nextAct = $id(lb.parentNode.parentNode.firstChild.innerHTML);
    var actid = $id("id_ipt_attr_id").value;
    
    var pathNextAct = $id(actid).querySelector(tagwfnextact + "[" + attrNextActId + "=" + nextAct.id + "]");
    pathNextAct.innerHTML = v;
    lb.innerHTML = v;
    $id(prefixwfLabId + actid + nextAct.id).innerHTML = v;
}

//-------------授权用户----------------------------
function modifyUsers() {
    var act = $id($id("id_ipt_attr_id").value);
    //只有一般活动或开始活动可以设置用户
    if (!act|| act.getAttribute(attrType) != typeCommon && act.getAttribute(attrType) != typeActStart) return; 

    window.open("Authors.aspx?curid=" + act.id, null, " height=420,width=700,  Left=300px,Top=20px, menubar=no,titlebar=no,scrollbar=no,toolbar=no, status=no,location=no");
}

//---------------活动对应模块----------------------------------
function setModule() {
    var dt = ns.GetModules().value;
    if (dt == null || dt.Rows.length < 1) return;
    
    $id("lb_ident").innerHTML = "MODULE";
    $id("id_div_left").innerHTML = "";
    $id("id_ul_right").innerHTML = "";
    
    var count = dt.Rows.length;
    var htms = "<ul id='id_ul_left' >";

    for (var i = 0; i < count; i++) {
        htms += "<li><a  id=\"" + dt.Rows[i].MID + "\" onclick=\"clickthis(this)\">" + dt.Rows[i].FILES + "(" + dt.Rows[i].MNAME + ")</a></li>";
    }
    htms += "</ul>";
    $id("id_div_left").innerHTML = htms;

    //--------------------------------------------------------------------------------------------------
    //--------将已经选择过的路径放在右边的ul中------------------------------------------------------------
    //--------------------------------------------------------------------------------------------------
    var act = $id($id("id_ipt_attr_id").value);
    if (act) {
        var wfModule=act.querySelector(tagwfModule);
        var modId = wfModule.querySelector(tagwfmodid).innerHTML;
        if (modId) {
            var uls = $id("id_ul_right");
            if (uls == null || !uls) return;

            var li = document.createElement("li");

            var a = document.createElement("a");
            a.id = modId;
            a.innerHTML = wfModule.querySelector(tagwfurl).innerHTML + "(" + wfModule.querySelector(tagwfdesc).innerHTML + ")";
            a.setAttribute("onclick", "clickthis(this)");
            a.onclick = function() {
                clickthis(a);
            };
            uls.appendChild(li.appendChild(a));
        }
    }
    //--------将已经选择过的路径放在右边的ul中----------------end -------------------------------------------
    $id("div_float_container").style.display = "block";
}

function btnModuleConfirm() {
    var act = $id($id("id_ipt_attr_id").value);
    var uls = $id("id_ul_right");
    if (!uls || uls.children.length > 2) return;
    var a = uls.firstChild.firstChild;
    var module = act.querySelector(tagwfModule);
    module.querySelector(tagwfmodid).innerHTML = a.id;
    module.querySelector(tagwfurl).innerHTML = a.innerHTML.split('(')[0];
    var vname = a.innerHTML.split('(')[1].split(')')[0];
    
    module.querySelector(tagwfdesc).innerHTML = vname;
    $id("id_ipt_attr_module").value = vname;
    $id("div_float_container").style.display = "none";
}

function div_float_close() {
    $id("div_float_container").style.display = "none";
}

function savePorcess() {
    convertToList();
}